<template>
  <div id="app">
    <button style="display: block; margin: 200px auto" v-if="flag1" @click="flag1 = !flag1"
      class="btn btn-primary">开始</button>
    <div v-else class="todo-container">
      <h3>待办事项</h3>
      <div class="todo-wrap">
        <MyHeader :addTodos="addTodo" />
        <MyFilter @changeFilter="changeFilter" />
        <MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo" />
        <MyFooter :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo" />
      </div>
    </div>
  </div>
</template>

<script>
import MyHeader from './components/MyHeader.vue';
import MyFilter from './components/MyFilter.vue';
import MyList from './components/MyList.vue';
import MyFooter from './components/MyFooter.vue';
export default {
  name: 'app',
  components: {
    MyHeader,
    MyFilter,
    MyList,
    MyFooter
  },
  data() {
    return {
      // 代办事项数组
      todos: [
        { id: '001', title: '吃饭', done: true },
        { id: '002', title: '写代码', done: false },
        { id: '003', title: '看电视', done: false }
      ],
      todos1: [],
      flag: '',
      flag1: true
    };
  },
  created() {
    this.todos1 = this.todos;
  },
  watch: {
    todos1() {
      console.log(this.todos1);
      if (this.flag === '') {
        this.todos = this.todos1
      } else {
        this.todos = this.todos1.filter(todo => todo.done == this.flag)
      }
    }
  },
  methods: {
    // 添加一个事件对象
    addTodo(todoObj) {
      this.todos1.unshift(todoObj)
      this.todos1 = [...this.todos1]
    },
    // 勾选或取消勾选
    checkTodo(id) {
      this.todos1 = this.todos1.map((todo) => ({ ...todo, done: todo.id === id ? !todo.done : todo.done }))
    },
    // 删除一个事件
    deleteTodo(id) {
      // filter方法会生成一个新数组，所以需要重新给todos赋值
      this.todos1 = this.todos1.filter((todo) => {
        return todo.id !== id;// 等于该id的会被过滤掉
      })
    },
    // 全选或全不选
    checkAllTodo(done) {
      this.todos1.forEach(todo => {
        todo.done = done;
      })
    },
    // 删除已完成的所有事件
    clearAllTodo() {
      this.todos1 = this.todos1.filter(todo => {
        return !todo.done;// 过滤掉done值为true的事件，留下done为false的事件
      })
    },
    changeFilter(item) {
      this.flag = item.flag;
      if (item.flag === '') {
        this.todos = this.todos1
      } else {
        this.todos = this.todos1.filter(todo => {
          return todo.done === item.flag;
        })
      }
    }

  }
}
</script>

<style>
body {
  background: #fff;
}

h3 {
  text-align: center;
  font-size: 25px;
}

input[type=checkbox] {
  width: 16px;
  height: 16px;
  margin-top: 2px;
  position: relative;
}

input[type=checkbox]::after {
  position: absolute;
  top: 0;
  color: #000;
  width: 16px;
  height: 16px;
  display: inline-block;
  visibility: visible;
  padding-left: 0px;
  text-align: center;
  content: ' ';
  border-radius: 3px;
}

input[type=checkbox]:checked::after {
  content: "✓";
  color: #fff;
  font-size: 10px;
  line-height: 15px;
  background-color: #67c23a;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  transition: all 0.5s;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-primary {
  width: 100px;
  height: 50px;
  font-size: 18px;
  color: #fff;
  background-color: #409eff;
  border-color: transparent;
  outline:unset;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 100px auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
